package com.ysd.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ysd.lis.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Component
public class IncrementalNumberGenerator {

    private static RedisTemplate<String, String> redisTemplate;
    private static  final String KEY_PREFIX = "incremental_number:";
    private static final String DATE_FORMAT = "yyyyMMdd";



    @Autowired
    public IncrementalNumberGenerator(RedisTemplate<String, String> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }
//报告模版生成code在用yyyyMMdd01 自增
    public static String generateIncrementalNumber() {
        LocalDate currentDate = LocalDate.now();
        String key = KEY_PREFIX + currentDate.format(DateTimeFormatter.ofPattern(DATE_FORMAT));

        Long number = redisTemplate.opsForValue().increment(key, 1);
        if (number == 1) {
            // 设置过期时间为下一天的凌晨
            Date tomorrowStart = Date.from(currentDate.plusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant());
            redisTemplate.expireAt(key, tomorrowStart);
        }

        return  currentDate.format(DateTimeFormatter.ofPattern(DATE_FORMAT)) + String.format("%02d", number);
    }
    //另存为报告模版时生成自定义报告名称在用yyyyMMdd001 自增

    public static String generateIncrementalNumber3() {
        LocalDate currentDate = LocalDate.now();
        String key = "REPORT"+ KEY_PREFIX + currentDate.format(DateTimeFormatter.ofPattern(DATE_FORMAT));

        Long number = redisTemplate.opsForValue().increment(key, 1);
        if (number == 1) {
            // 设置过期时间为下一天的凌晨
            Date tomorrowStart = Date.from(currentDate.plusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant());
            redisTemplate.expireAt(key, tomorrowStart);
        }

        return  currentDate.format(DateTimeFormatter.ofPattern(DATE_FORMAT)) + String.format("%03d", number);
    }

//LAB_RPTMAIN表中编号
    public static String generateIncrementalNumber4() {
        LocalDate currentDate = LocalDate.now();
        String key = "RPTMAIN"+KEY_PREFIX + currentDate.format(DateTimeFormatter.ofPattern(DATE_FORMAT));

        Long number = redisTemplate.opsForValue().increment(key, 1);
        if (number == 1) {
            // 设置过期时间为下一天的凌晨
            Date tomorrowStart = Date.from(currentDate.plusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant());
            redisTemplate.expireAt(key, tomorrowStart);
        }

        return  currentDate.format(DateTimeFormatter.ofPattern(DATE_FORMAT)) + String.format("%07d", number);
    }

//菜单添加生成的code
    public static String generateIncrementalNumber5() {
        LocalDate currentDate = LocalDate.now();
        String key = "MENU" + KEY_PREFIX + currentDate.format(DateTimeFormatter.ofPattern(DATE_FORMAT));

        Long number = redisTemplate.opsForValue().increment(key, 1);
        if (number == 1) {
            // 设置过期时间为下一天的凌晨
            Date tomorrowStart = Date.from(currentDate.plusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant());
            redisTemplate.expireAt(key, tomorrowStart);
        }

        return currentDate.format(DateTimeFormatter.ofPattern(DATE_FORMAT)) + String.format("%03d", number);
    }
//不合格原因组编码生成规则
    public static String generateIncrementalNumber6() {
        LocalDate currentDate = LocalDate.now();
        String key = "BHGJLZSZ" + KEY_PREFIX + currentDate.format(DateTimeFormatter.ofPattern(DATE_FORMAT));

        Long number = redisTemplate.opsForValue().increment(key, 1);
        if (number == 1) {
            // 设置过期时间为下一天的凌晨
            Date tomorrowStart = Date.from(currentDate.plusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant());
            redisTemplate.expireAt(key, tomorrowStart);
        }

        return  currentDate.format(DateTimeFormatter.ofPattern(DATE_FORMAT)) + String.format("%03d", number);
    }

    //不合格原因组编码生成规则
    public static String generateIncrementalNumber7() {
        LocalDate currentDate = LocalDate.now();
        String key = "BHGJLYYSZ" + KEY_PREFIX + currentDate.format(DateTimeFormatter.ofPattern(DATE_FORMAT));

        Long number = redisTemplate.opsForValue().increment(key, 1);
        if (number == 1) {
            // 设置过期时间为下一天的凌晨
            Date tomorrowStart = Date.from(currentDate.plusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant());
            redisTemplate.expireAt(key, tomorrowStart);
        }

        return  currentDate.format(DateTimeFormatter.ofPattern(DATE_FORMAT)) + String.format("%03d", number);
    }

    //标本交接单，批号
    public static String generateIncrementalNumber8() {
        LocalDate currentDate = LocalDate.now();
        String key = "BBJJD" + KEY_PREFIX + currentDate.format(DateTimeFormatter.ofPattern(DATE_FORMAT));

        Long number = redisTemplate.opsForValue().increment(key, 1);
        if (number == 1) {
            // 设置过期时间为下一天的凌晨
            Date tomorrowStart = Date.from(currentDate.plusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant());
            redisTemplate.expireAt(key, tomorrowStart);
        }

        return  currentDate.format(DateTimeFormatter.ofPattern(DATE_FORMAT)) + String.format("%03d", number);
    }





    public static  String getLatestGeneratedValue() {
        LocalDate currentDate = LocalDate.now();
        String key = KEY_PREFIX + currentDate.format(DateTimeFormatter.ofPattern(DATE_FORMAT));
        String value = redisTemplate.opsForValue().get(key);

        if (value != null) {
            return  value;
        } else {
            throw new RuntimeException("No value found for the key: " + key);
        }
    }










}
